package com.loongtech.bi.action.bipay;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.manager.flow.HadoopQueryManager;
import com.loongtech.bi.manager.pay.OverviewPayManager;
import com.loongtech.bi.manager.pay.OverviewPayManager.*;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import org.primefaces.event.TabChangeEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 付费分析 —付费收入Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("session")
@RequestMapping("/pay/overviewPay")
public class OverviewPayBiAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	public final static String platformCompareTab = "platform";  // 付费对比标签页-平台
	public final static String channelCompareTab = "channel";  // 付费对比标签页-渠道
	public final static String serverCompareTab = "server";  // 付费对比标签页-警服
	public String compareTab = platformCompareTab;  // 当前选中的付费对比标签页，默认是平台
	
	private OverviewPayData overviewPayData;  // 前端付费收入总体数据 
	private List<PayTrendData> payTrendData;  // 前端付费趋势数据
	private ChartUIData payTrendChart;  // 前端付费趋势图表
    private ChartUIData payTrendChart1;  // 前端付费趋势图表
    private ChartUIData payTrendChart2;  // 前端付费趋势图表
    private ChartUIData payTrendChart3;  // 前端付费趋势图表
    private ChartUIData payTrendChart4;  // 前端付费趋势图表

	private List<PayClassData> payClassData;  // 付费分类
	private List<ChartUIData> payClassChart;  
	
	private List<PayRangeData> payRangeData;  // 前端付费区间数据
	private ChartUIData payRangeChart;  // 前端付费区间图表
	private List<PayCompareData> payCompareData;  // 前端付费对比数据
	private ChartUIData payCompareChart;  // 前端付费对比图表
    private List<PayCompareData> payCompareData1;  // 前端付费对比数据
    private ChartUIData payCompareChart1;  // 前端付费对比图表
    private List<PayCompareData> payCompareData2;  // 前端付费对比数据
    private ChartUIData payCompareChart2;  // 前端付费对比图表
	
	@Autowired
	private OverviewPayManager overviewPayManager;

    @RequestMapping(value = "initParam.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void initParam(HttpServletRequest request, HttpServletResponse response, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("channelItmes", channelItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "search.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, @RequestParam String platform, @RequestParam String channel, @RequestParam String server, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            dateStart = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            dateEnd = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);

            queryPlatform = platform;
            queryChannel = channel;
            queryServer = server;

            overviewPayData = overviewPayManager.getOverviewPayData(dateStart, dateEnd, platform, channel, server);

            payTrendData = overviewPayManager.getPayTrendData(dateStart, dateEnd, platform, channel, server);
            if (payTrendData != null && payTrendData.size() > 0) {
                payTrendChart = new ChartUIData();
                payTrendChart.setJsonType(ChartType.AREA2.type);
                payTrendChart.setJsonStr(new ChartTool(payTrendData, PayTrendData.class, "payCount").loadData(ChartType.AREA.type));
                payTrendChart.setAllItmes(ChartTool.getAllItems(PayTrendData.class));

                payTrendChart1 = new ChartUIData();
                payTrendChart1.setJsonType(ChartType.AREA2.type);
                payTrendChart1.setJsonStr(new ChartTool(payTrendData, PayTrendData.class, "payUser").loadData(ChartType.AREA.type));
                payTrendChart1.setAllItmes(ChartTool.getAllItems(PayTrendData.class));

                payTrendChart2 = new ChartUIData();
                payTrendChart2.setJsonType(ChartType.AREA2.type);
                payTrendChart2.setJsonStr(new ChartTool(payTrendData, PayTrendData.class, "payInRadio").loadData(ChartType.AREA.type));
                payTrendChart2.setAllItmes(ChartTool.getAllItems(PayTrendData.class));

                payTrendChart3 = new ChartUIData();
                payTrendChart3.setJsonType(ChartType.AREA2.type);
                payTrendChart3.setJsonStr(new ChartTool(payTrendData, PayTrendData.class, "activeARPU").loadData(ChartType.AREA.type));
                payTrendChart3.setAllItmes(ChartTool.getAllItems(PayTrendData.class));

                payTrendChart4 = new ChartUIData();
                payTrendChart4.setJsonType(ChartType.AREA2.type);
                payTrendChart4.setJsonStr(new ChartTool(payTrendData, PayTrendData.class, "payARPU").loadData(ChartType.AREA.type));
                payTrendChart4.setAllItmes(ChartTool.getAllItems(PayTrendData.class));
            } else {
                payTrendChart = null;
                payTrendChart1 = null;
                payTrendChart2 = null;
                payTrendChart3 = null;
                payTrendChart4 = null;
            }

            // 付费分类
            payClassData = overviewPayManager.getPayClassData(dateStart, dateEnd, platform, channel, server);
            if (payClassData != null && payClassData.size() > 0) {
                payClassChart = new ArrayList<>();

                ChartUIData payClassChart0 = new ChartUIData();
                payClassChart0.setJsId("9988");
                payClassChart0.setJsonType(ChartType.PIE.type);
                payClassChart0.setJsonStr(new ChartTool(payClassData, PayClassData.class, "activeUser").loadData(ChartType.PIE.type));

                ChartUIData payClassChart1 = new ChartUIData();
                payClassChart1.setJsId("991");
                payClassChart1.setJsonType(ChartType.PIE.type);
                payClassChart1.setJsonStr(new ChartTool(payClassData, PayClassData.class, "payUser").loadData(ChartType.PIE.type));

                ChartUIData payClassChart2 = new ChartUIData();
                payClassChart2.setJsId("992");
                payClassChart2.setJsonType(ChartType.PIE.type);
                payClassChart2.setJsonStr(new ChartTool(payClassData, PayClassData.class, "payCount").loadData(ChartType.PIE.type));

                payClassChart.add(payClassChart0);
                payClassChart.add(payClassChart1);
                payClassChart.add(payClassChart2);
            } else {
                payClassChart = null;
            }

            // 付费对比
            payCompareData = overviewPayManager.getPayCompareData(platformCompareTab, dateStart, dateEnd, platform, channel, server);
            if (payCompareData != null && payCompareData.size() > 0) {
                payCompareChart = new ChartUIData();
                payCompareChart.setJsonType(ChartType.BAR.type);
                payCompareChart.setJsonStr(new ChartTool(payCompareData, PayCompareData.class, "").loadData(ChartType.BAR.type));
            } else {
                payCompareChart = null;
            }
            payCompareData1 = overviewPayManager.getPayCompareData(channelCompareTab, dateStart, dateEnd, platform, channel, server);
            if (payCompareData1 != null && payCompareData1.size() > 0) {
                payCompareChart1 = new ChartUIData();
                payCompareChart1.setJsonType(ChartType.BAR.type);
                payCompareChart1.setJsonStr(new ChartTool(payCompareData1, PayCompareData.class, "").loadData(ChartType.BAR.type));
            } else {
                payCompareChart1 = null;
            }
            payCompareData2 = overviewPayManager.getPayCompareData(serverCompareTab, dateStart, dateEnd, platform, channel, server);
            if (payCompareData2 != null && payCompareData2.size() > 0) {
                payCompareChart2 = new ChartUIData();
                payCompareChart2.setJsonType(ChartType.BAR.type);
                payCompareChart2.setJsonStr(new ChartTool(payCompareData2, PayCompareData.class, "").loadData(ChartType.BAR.type));
            } else {
                payCompareChart2 = null;
            }

            // updatePayRangeData();

            result.put("res", "0");
            result.put("overviewPayData", overviewPayData);
            result.put("payTrendData", payTrendData);
            result.put("payTrendChart", payTrendChart);
            result.put("payTrendChart1", payTrendChart1);
            result.put("payTrendChart2", payTrendChart2);
            result.put("payTrendChart3", payTrendChart3);
            result.put("payTrendChart4", payTrendChart4);
            result.put("payClassData", payClassData);
            result.put("payClassChart", payClassChart);
            result.put("payCompareData", payCompareData);
            result.put("payCompareChart", payCompareChart);
            result.put("payCompareData1", payCompareData1);
            result.put("payCompareChart1", payCompareChart1);
            result.put("payCompareData2", payCompareData2);
            result.put("payCompareChart2", payCompareChart2);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
	}
	
	public void onTabChange(TabChangeEvent event) {
		if ("付费区间".equals(event.getTab().getTitle())) {
			updatePayRangeData();
		}
    }
	
	// 付费区间
	public void updatePayRangeData() {
		payRangeData = null;
		payRangeChart = null;
		
		FacesContext context = FacesContext.getCurrentInstance();
		if (HadoopQueryManager.VISIT_QUEUE.offer("OverviewPayAction")) {
			System.out.println("VISIT_QUEUE offer OverviewPayAction ok!");
			payRangeData = overviewPayManager.getPayRangeData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
			if (payRangeData != null && payRangeData.size() > 0) {
				payRangeChart = new ChartUIData();
				payRangeChart.setJsonType(ChartType.BAR.type);
				payRangeChart.setJsonStr(new ChartTool(payRangeData, PayRangeData.class, "").loadData(ChartType.BAR.type));
			}
		} else {
			System.out.println("VISIT_QUEUE offer OverviewPayAction ok!");
			context.addMessage(null, new FacesMessage("提示", "系统繁忙，请稍后再试!"));
		}
	}

    @RequestMapping(value = "updateCompareType.do", method = {RequestMethod.GET, RequestMethod.POST})
	public void updateCompareType(HttpServletRequest request, HttpServletResponse response, @RequestParam String compareType, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (compareType != null && compareType.startsWith(channelCompareTab)) {
                compareTab = channelCompareTab;
            } else if (compareType != null && compareType.startsWith(serverCompareTab)) {
                compareTab = serverCompareTab;
            } else {
                compareTab = platformCompareTab;
            }

            payCompareData = overviewPayManager.getPayCompareData(compareTab, dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
            if (payCompareData != null && payCompareData.size() > 0) {
                payCompareChart = new ChartUIData();
                payCompareChart.setJsonType(ChartType.BAR.type);
                payCompareChart.setJsonStr(new ChartTool(payCompareData, PayCompareData.class, "").loadData(ChartType.BAR.type));
            }

            result.put("res", "0");
            result.put("payCompareData", payCompareData);
            result.put("payCompareChart", payCompareChart);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
    }

    @RequestMapping(value = "updateServer.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateServer(HttpServletRequest request, HttpServletResponse response, String server, String callback) {
        try {
            queryServer = server;
            selectedServer();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("channelItmes", channelItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updateChannel.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateChannel(HttpServletRequest request, HttpServletResponse response, String channel, String callback) {
        try {
            queryChannel = channel;
            updateServerItmes();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updatePlatform.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updatePlatform(HttpServletRequest request, HttpServletResponse response, String platform, String callback) {
        try {
            queryPlatform = platform;
            updateChannelItmes();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("channelItmes", channelItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @Override
    public void search() {

    }

    @Override
	protected String csvContent() {
		return null;
	}

	@Override
	protected String csvFileName() {
		return null;
	}

	public OverviewPayData getOverviewPayData() {
		return overviewPayData;
	}

	public void setOverviewPayData(OverviewPayData overviewPayData) {
		this.overviewPayData = overviewPayData;
	}

	public List<PayTrendData> getPayTrendData() {
		return payTrendData;
	}

	public void setPayTrendData(List<PayTrendData> payTrendData) {
		this.payTrendData = payTrendData;
	}

	public ChartUIData getPayTrendChart() {
		return payTrendChart;
	}

	public void setPayTrendChart(ChartUIData payTrendChart) {
		this.payTrendChart = payTrendChart;
	}

	public List<PayRangeData> getPayRangeData() {
		return payRangeData;
	}

	public void setPayRangeData(List<PayRangeData> payRangeData) {
		this.payRangeData = payRangeData;
	}

	public ChartUIData getPayRangeChart() {
		return payRangeChart;
	}

	public void setPayRangeChart(ChartUIData payRangeChart) {
		this.payRangeChart = payRangeChart;
	}

	public List<PayCompareData> getPayCompareData() {
		return payCompareData;
	}

	public void setPayCompareData(List<PayCompareData> payCompareData) {
		this.payCompareData = payCompareData;
	}

	public ChartUIData getPayCompareChart() {
		return payCompareChart;
	}

	public void setPayCompareChart(ChartUIData payCompareChart) {
		this.payCompareChart = payCompareChart;
	}

	public List<PayClassData> getPayClassData() {
		return payClassData;
	}

	public void setPayClassData(List<PayClassData> payClassData) {
		this.payClassData = payClassData;
	}

	public List<ChartUIData> getPayClassChart() {
		return payClassChart;
	}

	public void setPayClassChart(List<ChartUIData> payClassChart) {
		this.payClassChart = payClassChart;
	}

    public ChartUIData getPayTrendChart1() {
        return payTrendChart1;
    }

    public void setPayTrendChart1(ChartUIData payTrendChart1) {
        this.payTrendChart1 = payTrendChart1;
    }

    public ChartUIData getPayTrendChart2() {
        return payTrendChart2;
    }

    public void setPayTrendChart2(ChartUIData payTrendChart2) {
        this.payTrendChart2 = payTrendChart2;
    }

    public ChartUIData getPayTrendChart3() {
        return payTrendChart3;
    }

    public void setPayTrendChart3(ChartUIData payTrendChart3) {
        this.payTrendChart3 = payTrendChart3;
    }

    public ChartUIData getPayTrendChart4() {
        return payTrendChart4;
    }

    public void setPayTrendChart4(ChartUIData payTrendChart4) {
        this.payTrendChart4 = payTrendChart4;
    }

    public List<PayCompareData> getPayCompareData1() {
        return payCompareData1;
    }

    public void setPayCompareData1(List<PayCompareData> payCompareData1) {
        this.payCompareData1 = payCompareData1;
    }

    public ChartUIData getPayCompareChart1() {
        return payCompareChart1;
    }

    public void setPayCompareChart1(ChartUIData payCompareChart1) {
        this.payCompareChart1 = payCompareChart1;
    }

    public List<PayCompareData> getPayCompareData2() {
        return payCompareData2;
    }

    public void setPayCompareData2(List<PayCompareData> payCompareData2) {
        this.payCompareData2 = payCompareData2;
    }

    public ChartUIData getPayCompareChart2() {
        return payCompareChart2;
    }

    public void setPayCompareChart2(ChartUIData payCompareChart2) {
        this.payCompareChart2 = payCompareChart2;
    }
}
